package com.cuotigou.util;



import com.baidu.aip.ocr.AipOcr;
import com.cuotigou.pojo.Words;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/12/20.
 */
public class OcrUtil {
    private static Logger log = LoggerFactory.getLogger(OcrUtil.class);

    public static final String appId = "15217467";
    public static final String apiKey = "OyxcsBQIkow48ZVZ7or51c8S";
    public static final String secretKey = "z860vu6OglWGIL5F7lKzVshG9yolzeG1";

    /**
     * 百度ocr通用文字识别
     * @param imagePath
     * @return
     */
    public static String generalRecognition(String imagePath) {
        AipOcr client = new AipOcr(appId,apiKey,secretKey);

        // 自定义参数定义
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("language_type", "CHN_ENG");
        //调用sdk
        JSONObject response = client.basicGeneral(imagePath, options);
        log.info("返回的json:"+response.toString());
        //拼接为完整字符串
        List<Words> wordsList = JsonUtils.jsonToList(response.get("words_result").toString(), Words.class);

        StringBuilder builder = new StringBuilder();
        for (Words words:wordsList){
            builder.append(words.getWords()).append("\n");
        }
//        //删除文件
//        DeleteFileUtil.deleteFile(imagePath);
        return builder.toString();
    }

    public static void main(String[] args) {

        System.out.println( generalRecognition("d://1547451997503.jpg"));
    }
}
